<?php
namespace app\admin\controller;
use think\Controller;
use think\Cookie;
use think\Db;
header("content-type:text/html;charset=utf8");
class Login extends  Controller{
  public function index(){//
      return view("register/login");
    }
    public function admin(){
//        $cook = Cookie::get("admin_user");
        return view("Index/index");
    }
    public function information(){
        $data=Db::table("admin")->select();
        return view("register/information",array("aa"=>$data));
    }
    public function Register(){
        if (request()->isPost()) {//判断是否为POST方式提交qyqdfswj
            $date = [//通过数组的方式来赋值给字段
                'username' => $_POST["username"],
                'password' => md5($_POST["password"]),
                'email' => $_POST["email"],
            ];
            $date["time"]=date("Y-m-d H-i-s",time());
            $yzm=$_POST["yzm"];
            if (empty($date["username"]) && empty($date["password"]) && empty($_POST["email"]) && empty($_POST["yzm"])) {
                //判断提交的数据是有值，有值返回true无值返回false
                $this->error("用户名和密码为空，请重新注册");
            } else {
                if (empty($_POST["username"])) {
                    $this->error("用户名为空，请重新注册");
                } else {
                    if (empty($_POST["password"])) {
                        $this->error("密码为空，请重新注册");
                    } else {
                        if ($_POST["password"] != $_POST["re_password"]) {
                            $this->error("密码不一致，请重新注册");
                        } else {
                            if(!captcha_check($yzm)){
//                                $this->error("验证码错误");
                            }else{
                                if (empty($_POST["email"])) {
                                    $this->error("邮箱不能为空，请重新注册");
                                } else {
                                    //把数据添加到数据库
                                    $data = Db::table("admin")->insert($date);
                                    if ($data) {
                                        $this->success("新增成功");
                                    } else {
                                        // 上传失败获取错误信息
                                        $this->error('新增失败');
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }
    }

    public function checklogin(){
        if (request()->isPost()) {
            $pass =  md5($_POST["password"]);
            Cookie::set("admin_user",$_POST["username"]);
            $cook = $_POST["username"];
//            dump($_POST);
            if (request()->isPost()) {//判断是否post提交
                $date = Db::table("admin")->select();
//                dump($date);
                if($date[0]["username"] == $cook && $date[0]["password"] == $pass){
                    $this->success("登录成功","Index/admin",'',1);
                }else{
                    $this->redirect("Login/index");
                }
            } else {

            }
        }
    }
    public function delete(){
        $id=$_GET["id"];
        $data = Db::table("admin")->where("id=$id")->delete();
        if($data){
            $this->success("删除成功","Login/information");
        }else{
            $this->error("删除失败","Login/information");
        }
    }

    public function update()
    {//修改数据
        if (request()->isPost()) {
            $get = $_GET["id"];//获取ID值
            $date = array('password' =>$password=md5($_POST["re_password"]));//通过数组的方式来赋值给字段
            $data = DB::table("admin")->where("id=".$get)->update($date);//修改数据
            if ($data){
                $this->success("修改成功","Login/index");
            } else {
                $this->error("修改失败");
            }
        }
    }

}